<template>
  <el-card class="box-card">
    <el-form ref="form" :model="form" label-width="130px">
      <el-form-item label="平台名称">
        <el-input v-model="form.Name"></el-input>
      </el-form-item>
      <el-form-item label="AppId">
        <el-input v-model="form.WeixinAppId"></el-input>
      </el-form-item>
      <el-form-item label="AppSecret">
        <el-input v-model="form.WeixinAppSecret"></el-input>
      </el-form-item>
      <el-form-item label="Token">
        <el-input v-model="form.WeixinToken"></el-input>
      </el-form-item>
      <el-form-item label="EncodingAESKey">
        <el-input v-model="form.WeixinEncodingAESKey"></el-input>
      </el-form-item>
      <el-form-item label="回调地址">
        <el-input v-model="form.CallbackUrl"></el-input>
      </el-form-item>
      <el-form-item label="IsEnable">
        <el-switch v-model="form.IsEnable"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="savePlatform">保存信息</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
const detaulInfo = {
  Id: 0,
  Name: "",
  WeixinAppId: "",
  WeixinAppSecret: "",
  WeixinToken: "",
  WeixinEncodingAESKey: "",
  CallbackUrl: "",
  IsEnable: true
};
export default {
  name: "PlatformDetail",
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: Object.assign({}, detaulInfo)
    };
  },
  created() {
    if (this.isEdit) {
      this.form.Id = this.$route.params.id || 0;
      this.getPlatformInfo();
    } else {
      this.form = Object.assign({}, detaulInfo);
    }
  },
  mounted() {},
  methods: {
    onSubmit() {
      console.log("submit!");
    },
    async getPlatformInfo() {
      let result = await this.$request.platform.getPlatformInfo({
        id: this.form.Id
      });
      Object.assign(this.form, result.DataValue);
      this.setTagsViewTitle();
    },
    async savePlatform() {
      var url = this.isEdit ? "savePlatformInfo" : "addPlatformInfo";
      let result = await this.$request.platform[url](this.form);
      if (result.Success) {
        this.$message({
          message: "保存成功",
          type: "success",
          showClose: true,
          duration: 1000
        });
        if (!this.isEdit) {
          this.form = Object.assign({}, this.defaultInfo);
        }
      } else {
        this.$Notice.success({ title: result.Info });
      }
    },
    setTagsViewTitle() {
      const title = `编辑平台-${this.form.Id}`;
      console.log(title);
      const route = Object.assign({}, Object.assign({}, this.$route), {
        title: title
      });
      this.$store.dispatch("tagsView/updateVisitedView", route);
    }
  }
};
</script>

<style></style>
